package routes

import (
	"github.com/gin-gonic/gin"
	"iot-base/auth/api/handler/alarm"
	"iot-base/auth/api/handler/auth"
	"iot-base/auth/api/handler/company"
	"iot-base/auth/api/handler/file"
	"iot-base/auth/api/handler/station"
	"iot-base/auth/api/handler/thing"
	"iot-base/auth/api/handler/user"
	"iot-base/auth/api/logic"
	"iot-base/auth/config"
	auth2 "iot-base/auth/middleware/auth"
	"net/http"
)

func InitRouter(g *gin.Engine) {
	//g.GET("/login", func(c *gin.Context) {
	//	c.Redirect(http.StatusMovedPermanently, "/")
	//})
	g.MaxMultipartMemory = 8 << 20 // 限制内存大小 8M
	g.StaticFS("/static", http.Dir(config.Conf.Server.UploadPath))
	dir := http.Dir("./static")
	g.Static("/lib", string(dir)+"/lib")
	g.StaticFS("/assets", dir+"/assets")
	g.LoadHTMLFiles(string(dir) + "/index.html")
	g.GET("/", func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", nil)
	})
	routeVersion := g.Group(config.Conf.Server.Prefix + "/" + config.Conf.Server.Version)
	routeVersion.POST("/upload", file.UploadImg)
	routeVersion.POST("/login", user.Login) // middleware.OperationLogsMiddleware() 请求日志
	//routeVersion.GET("/ws", ws.Ws)
	routeVersion.GET("/conf/exportMenu", file.ExportMenu) // ImportMenu
	routeVersion.POST("/conf/ImportMenu", file.ImportMenu)
	// 鉴权
	routeVersion.Use(auth2.JWTAuth())
	routeVersion.POST("/signOut", user.SignOut) // middleware.OperationLogsMiddleware() 请求日志
	routeVersion.POST("/refreshToken", user.RefreshToken)
	routeVersion.GET("/operationLogs", user.OperationLogs)
	// ws
	authGroup := routeVersion.Group("/auth")
	{
		// 用户
		authGroup.Any("/user", user.UserAny)
		authGroup.GET("/user/:id", user.DetailUser)
		authGroup.PUT("/resetPwd", user.ResetPwd)
		authGroup.PUT("/changePwd", user.ChangePwd)
		// 权限角色
		authGroup.GET("/role", auth.FindRole)
		authGroup.POST("/role", auth.AddRole)
		authGroup.PUT("/role", auth.EditRole)
		authGroup.DELETE("/role", auth.DelRole)
		// 权限组
		authGroup.GET("/group", auth.FindGroup)
		authGroup.POST("/group", auth.AddGroup)
		authGroup.PUT("/group", auth.EditGroup)
		authGroup.DELETE("/group", auth.DelGroup)
		// 权限规则 (菜单)
		authGroup.GET("/rule", auth.FindRule)
		authGroup.POST("/rule", auth.AddRule)
		authGroup.PUT("/rule", auth.EditRule)
		authGroup.DELETE("/rule", auth.DelRule)
		authGroup.GET("/menu", auth.Menu)
		authGroup.GET("/groupMenu", auth.GroupMenu)
		// 用户组
		authGroup.GET("/userGroup", auth.FindUserGroup)
		authGroup.PUT("/userGroup", auth.EditUserGroup)
		// 站点
		authGroup.GET("/station", station.ListStations)
		authGroup.POST("/station", station.AddStations)
		authGroup.GET("/userStation", station.GetUserStation)
		// 设备权限
		authGroup.PUT("/groupStation", station.EditGroupStations)
		authGroup.GET("/video", thing.Video)
		authGroup.GET("/device", thing.DeviceList)
		// 公司信息
		authGroup.GET("/company", company.List)
		authGroup.POST("/company", company.Add)
		authGroup.DELETE("/company", company.Del)
		authGroup.PUT("/company", company.Update)
	}
	// onvif协议
	onvif := routeVersion.Group("onvif")
	{
		onvif.GET("/GetProfiles", thing.GetProfiles)
		onvif.PUT("/GetProfiles", thing.SetProfile)
		onvif.POST("/move", thing.ContinuousMove)
		onvif.POST("/stop", thing.StopMove)
		onvif.GET("/preset", thing.GetPresets)
		onvif.POST("/preset", thing.SetPreset)
		onvif.DELETE("/preset", thing.RemovePreset)
		onvif.POST("/GotoPreset", thing.GotoPreset)
		onvif.POST("/FocusMove", thing.FocusMove)
	}
	// 告警
	alarmRoute := routeVersion.Group("alarm")
	{
		alarmRoute.GET("/active", alarm.ActiveAlarm)
		alarmRoute.GET("/history", alarm.HistoryAlarm)
	}
	// thing
	thingRoute := routeVersion.Group("thing")
	{
		thingRoute.GET("/allInfo", thing.GetStationThingInfo)
		thingRoute.GET("/dataView", thing.GetHomeThingData)
		thingRoute.GET("/his", thing.QueryHis)
		thingRoute.GET("/exportHis", thing.ExportHis)
		thingRoute.GET("/data", thing.QueryData)
		thingRoute.POST("/service", thing.GetService)
	}
	// control 控制
	controlRoute := routeVersion.Group("control")
	{
		controlRoute.POST("/accessControl", thing.AccessControl)
		controlRoute.POST("/fan", thing.Fan)       //风机
		controlRoute.POST("/switch", thing.Switch) //开关
		controlRoute.POST("/acSwitch", thing.AirConditioningSwitch)
		controlRoute.POST("/acDispatch", thing.AirConditioningDispatch)
		controlRoute.POST("/cmd", thing.Cmd)
		//controlRoute.GET("/thingStatus", thing.GetThingStatus)
		// 校准接口，这里需要区分s设备端和云端
		addCaliRoute(controlRoute)
	}
	// event  事件
	eventRoute := routeVersion.Group("event")
	{
		eventRoute.GET("history", alarm.HistoryEvent)
	}
	// 任务
	addTaskRoute(routeVersion)
	//初始化上传数据平台任务
	logic.InitPlatform()
	//上报平台创建
	platformRoute := routeVersion.Group("platform")
	{
		platformRoute.POST("/add", logic.AddPlatform)
		platformRoute.POST("/del", logic.DelPlatform)
		platformRoute.POST("/modify", logic.ModifyPlatform)
		platformRoute.GET("/platformList", logic.ListPlatform)
		platformRoute.GET("/list", logic.PlatformStyle)
	}
	//回补数据
	reData := routeVersion.Group("reData")
	{
		reData.POST("/query", logic.QueryReData)
		reData.POST("/queryStyle", logic.QueryReDataByStyle)
		reData.GET("/report", logic.ReportReData)
	}
	// conf
	conf := routeVersion.Group("conf")
	{
		conf.GET("/get", file.GetConf)
		conf.POST("/set", file.SetConf)
	}
	fileRoute := routeVersion.Group("file")
	{
		fileRoute.GET("/getPic", file.GetPic)
		fileRoute.GET("/img", file.ShowImg)
	}
}
